﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class UserControls_AwardsUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.UpdateParameters["description"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.UpdateParameters["awardingBody"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.UpdateParameters["dateAwarded"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.InsertParameters["description"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.InsertParameters["awardingBody"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Awards_ObjectDataSource.InsertParameters["dateAwarded"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgAward_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Awards_ObjectDataSource.UpdateParameters["awardsKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("awardsKey"));
            Awards_ObjectDataSource.UpdateParameters["description"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("description_textbox"));
            Awards_ObjectDataSource.UpdateParameters["awardingBody"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("awarding_body_textbox"));
            Awards_ObjectDataSource.UpdateParameters["dateAwarded"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("date_awarded_datepicker"));

            if (ValidateFormFields("UPDATE"))
            {
                Awards_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("AwardsMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            Awards_ObjectDataSource.DeleteParameters["awardsKey"].DefaultValue = item["AwardsKey_column"].Text;
            Awards_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            Awards_ObjectDataSource.InsertParameters["description"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("description_textbox"));
            Awards_ObjectDataSource.InsertParameters["awardingBody"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("awarding_body_textbox"));
            Awards_ObjectDataSource.InsertParameters["dateAwarded"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("date_awarded_datepicker"));

            if (ValidateFormFields("INSERT"))
            {
                Awards_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("AwardsMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
            
        }
    }
}